Concurrent, Real-Time and Distributed Programming in Java by Badr Benmammar

Concurrent, Real-Time and Distributed Programming in Java by Badr Benmammar

Author:Badr Benmammar [Неизв.]
Language: eng
Format: epub
ISBN: 9781119482758
Publisher: John Wiley & Sons, Inc.
Published: 2017-12-13T00:00:00+00:00


Execution:

Server side:

connected

received: A

Client side:

server -> client: Student: A GL: 13

4.2.1.8. Communications between a Java applet and a server using sockets

The following execution shows a communication between an applet and a server using sockets:

Figure 4.4. Communication between an applet and a server with the Sockets. For a color version of the figure, see www.iste.co.uk/benmammar/java.zip

The implementation of the server is as follows:

import java.io.*; import java.net.*; import java.util.*; public class NetServer { public static void main(String[] args) { try{ ServerSocket serverSocket = new ServerSocket(8765); Socket clientSocket = serverSocket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); while (true) { Integer numb = new Integer(in.readLine()); System.out.println("recu: " + numb.intValue()); out.println(numb.intValue()+1); } }catch(IOException ex){System.err.println(ex);} } }

The implementation of the client is as follows:

import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class NetApplet extends Applet implements ActionListener { TextField numbField; Label display; Socket socket; public void init() { try { socket = new Socket("localhost",8765); } catch (UnknownHostException e) {System.out.println("Unknown host");} catch (IOException e) { System.out.println("IO Exception");} numbField = new TextField(6); add(numbField); Button button = new Button("Send"); add(button); button.addActionListener(this); display = new Label("Pas de nombre"); add(display); } public void actionPerformed(ActionEvent e) { int numb = 0; String numbStr = null; BufferedReader in = null; PrintWriter out = null; String actionCommand = e.getActionCommand(); // know the source of the event if (e.getSource() instanceof Button) if (actionCommand.equals("Send")){ try {numb = Integer.parseInt(numbField.getText());} catch (NumberFormatException ex) {System.out.println ("Number Format Exception");} try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); } catch (IOException ex) {System.out.println("IO Exception");} out.println(numb); try {numbStr = in.readLine();} catch (IOException ex) {System.out.println("Applet receive failed:");} display.setText(numbStr); } } }// end of class



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.